<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
            <el-breadcrumb-item>商品管理</el-breadcrumb-item>
            <el-breadcrumb-item>商品分类</el-breadcrumb-item>
        </el-breadcrumb>

        <el-card>
            <el-row>
                <el-col>
                    <el-button type="primary" @click="showAddCateDialog">添加分类</el-button>
                </el-col>
            </el-row>
        </el-card>

        <!-- 表格区 -->
        <tree-table :data="catelist" :columns="columns" 
        :selection-type="false" :expand-type="false" 
        show-index index-text="" border>
            <template slot="isok" slot-scope="scope">
                <i class="el-icon-error" v-if="scope.row.cat_deleted===true" style="color:red"></i>
                <i class="el-icon-success" v-else style="color:green"></i>
            </template>
            <!-- 排序 -->
            <template slot="order" slot-scope="scope">
                <el-tag size="mini" v-if="scope.row.cat_level===0">一级</el-tag>
                <el-tag size="mini" type="success" v-else-if="scope.row.cat_level===1">二级</el-tag>
                <el-tag size="mini" type="warning" v-else>三级</el-tag>
            </template>
            <!-- 操作 -->
            <template slot="opt" slot-scope="scope">
                <el-button type="primary" icon="el-icon-edit" size="mini">编辑</el-button>
                <el-button type="danger" icon="el-icon-delete" size="mini">删除</el-button>
            </template>
        </tree-table>

        <!-- 分页区 -->
        <el-pagination @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="querInfo.pagenum"
      :page-sizes="[3, 5, 7, 9]"
      :page-size="querInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total">
    </el-pagination>
        <!-- 添加分类对话框 -->
        <el-dialog title="添加分类" :visible.sync="CateDialogVisible" width="50%" @close="addCateDialogVisible">
        <!-- 添加分类表单 -->
        <el-form :model="addCateForm" :rules="addCateFormRules" 
        ref="addCateFormRef" label-width="100px">
            <el-form-item label="分类名称：" prop="cat_name">
            <el-input v-model="addCateForm.cat_name"></el-input>
            </el-form-item>
            <el-form-item label="父级分类：" >
                <!-- options用来指定数据源 v-model保存选中得数组  props用来指定配置对象 很重要-->
                <el-cascader v-model="selectedKeys" :options="parentCateList" 
                :props="cascaderProps"  @change="parentCateChange" clearable change-on-select >
                </el-cascader>
            </el-form-item>
        </el-form> 
        <span slot="footer" class="dialog-footer">
            <el-button @click="CateDialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="addCate">确 定</el-button>
        </span>
        </el-dialog>


    </div>
</template>

<script>
export default {
    data(){
        return{
            catelist:[],
            querInfo:{
                type:3,
                pagenum:1,
                pagesize:5,
            },
            total:0,
            // 为table指定列的定义
            CateDialogVisible : false,
            columns:[{
                label:'分类名称',
                prop:'cat_name'
            },{
                label:'是否有效',
                type:'template',
                template:'isok'
            },
            {
                label:'排序',
                type:'template',
                template:'order'
            },
            {
                label:'操作',
                type:'template',
                template:'opt'
            },],

            // 表单校验对象
            addCateForm:{
                cat_name:'',
                cat_pid: 0,
                cat_level:0
            },
            addCateFormRules:{
                cat_name:[
                {required:true,message:'请输入分类的名称',trigger:'blur'}
                    ],
            },
            parentCateList:[],
            cascaderProps:{
                // 存取你点的哪个 一般用id来存储
                value: 'cat_id',
                // 你看的得这个值是什么名字
                label: 'cat_name',
                // 用什么来存取下一级
                children:'children',
                expandTrigger: 'hover' 
            },
            selectedKeys:[],

        }
    },
    created(){
        this.getCateList()
    },
    methods:{
      async  getCateList(){
          const {data:res} = await this.$http.get('categories',{params:this.querInfo})
          if(res.meta.status!==200) return this.$message.error('失败')
          this.catelist = res.data.result
        //   console.log(this.catelist)
          this.total = res.data.total
        //    console.log(this.total)
        },
        handleSizeChange(newSize){
            this.querInfo.pagesize = newSize
            this.getCateList()
        },
        handleCurrentChange(newPage){
            this.querInfo.pagenum = newPage
            this.getCateList()
        },
        showAddCateDialog(){
            this.getParentCateList()
            this.CateDialogVisible = true
        },
        // 获取父级分类列表
       async getParentCateList(){
            const {data:res} = await this.$http.get('categories',{params:{type:2}})
            if(res.meta.status!==200) return this.$message.error('获取失败  ')
            this.parentCateList = res.data
            console.log(this.parentCateList)
        },
        parentCateChange(){
            if(this.selectedKeys.length > 0){
                this.addCateForm.cat_pid = this.selectedKeys[this.selectedKeys.length-1]
                this.addCateForm.cat_level = this.selectedKeys.length
                return
            }else {
                this.addCateForm.cat_pid = 0
                this.addCateForm.cat_level = 0 
            }
        },
        addCate(){
        //    表单预校验 ref是引用对象
            this.$refs.addCateFormRef.validate( async valid => {
                if(!valid) return 
               const {data: res} = await this.$http.post('categories',this.addCateForm)
                if(res.meta.status!==201) return this.$message.error('错了')
                this.$message.success('添加成功')
                this.getCateList()
           })

           

           this.CateDialogVisible = false
        },
        // 监听对话框关闭事件，重置表单数据
        addCateDialogVisible(){
            this.$refs.addCateFormRef.resetFields()
            this.selectedKeys = []
            this.addCateForm.cat_level = 0
            this.addCateForm.cat_pid = 0
        },

    
    },


}


</script>

<style scoped>
.el-card{
    margin-top: 20px;
}
</style>